/**
 * Copyright:Copyright(c)2014-2017
 * Company:厦门市易致达物联网科技有限公司
 *
 * @version 1.0
 */
package shop.controller.manage.handlelog;

import shop.core.common.bean.JSONResult;
import shop.core.common.bean.ManageContainer;
import shop.core.common.dao.page.PagerModel;
import shop.core.util.KeyValueHelper;
import shop.core.Services;
import shop.core.util.MD5;
import shop.services.manage.handlelog.HandlelogService;
import shop.services.manage.handlelog.bean.Handlelog;
import shop.core.BaseController;
import shop.core.RequestHolder;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import shop.services.manage.proxy.ProxyService;
import shop.services.manage.proxy.bean.Proxy;
import shop.services.manage.proxycentral.ProxycentralService;
import shop.services.manage.proxycentral.bean.Proxycentral;
import shop.services.manage.proxycentrall.ProxycentrallService;
import shop.services.manage.proxycentrall.bean.Proxycentrall;
import shop.services.manage.proxycentralll.ProxycentralllService;
import shop.services.manage.proxycentralll.bean.Proxycentralll;
import shop.services.manage.proxycollection.ProxycollectionService;
import shop.services.manage.proxycollection.bean.Proxycollection;
import shop.services.manage.proxyexperiment.ProxyexperimentService;
import shop.services.manage.proxyexperiment.bean.Proxyexperiment;
import shop.services.manage.proxyfirstcheck.ProxyfirstcheckService;
import shop.services.manage.proxyfirstcheck.bean.Proxyfirstcheck;
import shop.services.manage.proxysecondcheck.ProxysecondcheckService;
import shop.services.manage.proxysecondcheck.bean.Proxysecondcheck;
import shop.services.manage.system.bean.User;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @类名称：HandlelogAction
 * @创建人：wzl
 * @创建时间：2018-05-24 下午14:52:56
 * @版本号：1.0
 * @描述：
 */
@Controller
@RequestMapping("/manage/handlelog/")
public class HandlelogAction extends BaseController<Handlelog> {
    private static final Logger logger = LoggerFactory.getLogger(HandlelogAction.class);
    @Autowired
    private HandlelogService handlelogService;
    @Autowired
    private ProxyService proxyService;
    @Autowired
    private ProxycentralService proxycentralService;
    @Autowired
    private ProxycentrallService proxycentrallService;
    @Autowired
    private ProxycollectionService proxycollectionService;
    @Autowired
    private ProxyexperimentService proxyexperimentService;
    @Autowired
    private ProxyfirstcheckService proxyfirstcheckService;
    @Autowired
    private ProxysecondcheckService proxysecondcheckService;
    @Autowired
    private ProxycentralllService proxycentralllService;
    private static final String page_toList = "/manage/handlelog/handlelogList";

    public HandlelogService getService() {
        return handlelogService;
    }

    private HandlelogAction() {
        super.page_toList = page_toList;
        super.page_toAdd = null;
        super.page_toEdit = null;
    }

    /**
     * 分页查詢事件追踪列表
     *
     * @param request   请求
     * @param handlelog 对象参数
     * @return 分页数据模型
     */
    @RequestMapping("selectHandleLogPage")
    @ResponseBody
    public PagerModel selectHandleLogPage(HttpServletRequest request, Handlelog handlelog) {
        int offset = 0;
        int pageSize = 10;
        int start = Integer.parseInt(request.getParameter("start"));
        int length = Integer.parseInt(request.getParameter("length"));
        if (start > 0)
            offset = start;
        if (length > 0)
            pageSize = length;
        handlelog.setOffset(offset);
        handlelog.setPageSize(pageSize);
        //分页查詢事件追踪列表
        PagerModel pager = handlelogService.selectHandleLogPage(handlelog);
        pager.setRecordsTotal(pager.getTotal());
        pager.setRecordsFiltered(pager.getTotal());
        return pager;
    }

    /**
     * 业务组添加事件跟踪记录(指派中枢人员)
     *
     * @return jsonResult
     * @throws Exception
     */
    @RequestMapping(value = "insertHandleLog", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult insertHandleLog(HttpSession session, Handlelog handlelog, String userID) throws Exception {
        JSONResult jsonResult = new JSONResult();
        User user = (User) session.getAttribute(ManageContainer.manage_session_user_info);
        handlelog.setUserID(user.getId());
        String proxyID = handlelog.getProxyID();
        Proxy proxy = new Proxy();
        proxy.setId(proxyID);
        proxy.setRemark(handlelog.getRemark());
        proxy.setStatus("等待指派采样人员");
        handlelogService.insert(handlelog);
        proxyService.update(proxy);
        String[] userIDS = userID.split(",");
        for (int i = 0; i < userIDS.length; i++) {
            Proxycentral proxycentral = new Proxycentral();
            proxycentral.setProxyID(proxyID);
            proxycentral.setUserID(userIDS[i]);
            Proxycentral rs = new Proxycentral();
            rs.setProxyID(proxyID);
            rs.setUserID(userIDS[i]);
            rs = proxycentralService.selectOne(rs);
            if (rs == null)
                proxycentralService.insert(proxycentral);
        }
        return jsonResult;
    }

    /**
     * 中枢岗添加事件跟踪记录（指派采集人员）
     *
     * @return jsonResult
     * @throws Exception
     */
    @RequestMapping(value = "insertCentralHandleLog", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult insertCentralHandleLog(HttpSession session, Handlelog handlelog, String userID) throws Exception {
        JSONResult jsonResult = new JSONResult();
        User user = (User) session.getAttribute(ManageContainer.manage_session_user_info);
        handlelog.setUserID(user.getId());
        String proxyID = handlelog.getProxyID();
        Proxy proxy = new Proxy();
        proxy.setId(proxyID);
        proxy.setRemark(handlelog.getRemark());
        if ("提交".equals(handlelog.getActionName())) {
            proxy.setStatus("采样中");
            String[] userIDS = userID.split(",");
            for (int i = 0; i < userIDS.length; i++) {
                Proxycollection proxycollection = new Proxycollection();
                proxycollection.setProxyID(proxyID);
                proxycollection.setUserID(userIDS[i]);
                Proxycollection rs = new Proxycollection();
                rs.setProxyID(proxyID);
                rs.setUserID(userIDS[i]);
                rs = proxycollectionService.selectOne(rs);
                if (rs == null)
                    proxycollectionService.insert(proxycollection);
            }
        } else {
            proxy.setStatus("等待提交委托书");
        }
        handlelogService.insert(handlelog);
        proxyService.update(proxy);
        return jsonResult;
    }

    /**
     * 中枢岗添加事件跟踪记录（指派实验人员）
     *
     * @return jsonResult
     * @throws Exception
     */
    @RequestMapping(value = "insertCentrallHandleLog", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult insertCentrallHandleLog(HttpSession session, Handlelog handlelog, String userID) throws Exception {
        JSONResult jsonResult = new JSONResult();
        User user = (User) session.getAttribute(ManageContainer.manage_session_user_info);
        handlelog.setUserID(user.getId());
        String proxyID = handlelog.getProxyID();
        Proxy proxy = new Proxy();
        proxy.setId(proxyID);
        proxy.setRemark(handlelog.getRemark());
        if ("提交".equals(handlelog.getActionName())) {
            proxy.setStatus("实验分析");
            String[] userIDS = userID.split(",");
            for (int i = 0; i < userIDS.length; i++) {
                Proxyexperiment proxyexperiment = new Proxyexperiment();
                proxyexperiment.setProxyID(proxyID);
                proxyexperiment.setUserID(userIDS[i]);
                Proxyexperiment rs = new Proxyexperiment();
                rs.setProxyID(proxyID);
                rs.setUserID(userIDS[i]);
                rs = proxyexperimentService.selectOne(rs);
                if (rs == null)
                    proxyexperimentService.insert(proxyexperiment);
            }
        } else {
            proxy.setStatus("采样中");
        }
        handlelogService.insert(handlelog);
        proxyService.update(proxy);
        return jsonResult;
    }

    /**
     * 中枢岗添加事件跟踪记录（确定项目是否完成）
     *
     * @return jsonResult
     * @throws Exception
     */
    @RequestMapping(value = "insertCentralHandleLogs", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult insertCentralHandleLogs(HttpSession session, Handlelog handlelog) throws Exception {
        JSONResult jsonResult = new JSONResult();
        User user = (User) session.getAttribute(ManageContainer.manage_session_user_info);
        handlelog.setUserID(user.getId());
        String proxyID = handlelog.getProxyID();
        Proxy proxy = new Proxy();
        proxy.setId(proxyID);
        proxy.setRemark(handlelog.getRemark());
        if ("完成".equals(handlelog.getActionName())) {
            proxy.setStatus("已完成");
        } else {
            proxy.setStatus("二审中");
        }
        handlelogService.insert(handlelog);
        proxyService.update(proxy);
        return jsonResult;
    }

    /**
     * 中枢岗变更采集人员信息
     *
     * @return jsonResult
     * @throws Exception
     */
    @RequestMapping(value = "changeCollector", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult changeCollector(HttpSession session,Handlelog handlelog, String userID) throws Exception {
        JSONResult jsonResult = new JSONResult();
        //添加事件跟踪记录
        User user = (User) session.getAttribute(ManageContainer.manage_session_user_info);
        handlelog.setUserID(user.getId());
        handlelog.setActionName("变更采集人员");
        handlelogService.insert(handlelog);
        //删除采集人员信息
        String proxyID=handlelog.getProxyID();
        Proxycollection proxycollection = new Proxycollection();
        proxycollection.setProxyID(proxyID);
        List<Proxycollection> rsList = proxycollectionService.selectList(proxycollection);
        for (Proxycollection proxycollectionInfo : rsList) {
            proxycollectionService.delete(proxycollectionInfo);
        }
        //变更采集人员信息
        String[] userIDS = userID.split(",");
        for (int i = 0; i < userIDS.length; i++) {
            Proxycollection collection = new Proxycollection();
            collection.setProxyID(proxyID);
            collection.setUserID(userIDS[i]);
            proxycollectionService.insert(collection);
        }
        return jsonResult;
    }

    /**
     * 采集组添加事件跟踪记录（指派中枢人员）
     *
     * @return jsonResult
     * @throws Exception
     */
    @RequestMapping(value = "insertCollectHandleLog", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult insertCollectHandleLog(HttpSession session, Handlelog handlelog, String userID) throws Exception {
        JSONResult jsonResult = new JSONResult();
        User user = (User) session.getAttribute(ManageContainer.manage_session_user_info);
        handlelog.setUserID(user.getId());
        String proxyID = handlelog.getProxyID();
        Proxy proxy = new Proxy();
        proxy.setId(proxyID);
        proxy.setRemark(handlelog.getRemark());
        if ("提交".equals(handlelog.getActionName())) {
            proxy.setStatus("等待指派实验人员");
            String[] userIDS = userID.split(",");
            for (int i = 0; i < userIDS.length; i++) {
                Proxycentrall proxycentrall = new Proxycentrall();
                proxycentrall.setProxyID(proxyID);
                proxycentrall.setUserID(userIDS[i]);
                Proxycentrall rs = new Proxycentrall();
                rs.setProxyID(proxyID);
                rs.setUserID(userIDS[i]);
                rs = proxycentrallService.selectOne(rs);
                if (rs == null)
                    proxycentrallService.insert(proxycentrall);
            }
        } else {
            proxy.setStatus("等待指派采样人员");
        }
        handlelogService.insert(handlelog);
        proxyService.update(proxy);
        return jsonResult;
    }

    /**
     * 实验组添加事件跟踪记录
     *
     * @return jsonResult
     * @throws Exception
     */
    @RequestMapping(value = "insertExperimentHandleLog", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult insertExperimentHandleLog(HttpSession session, Handlelog handlelog) throws Exception {
        JSONResult jsonResult = new JSONResult();
        User user = (User) session.getAttribute(ManageContainer.manage_session_user_info);
        handlelog.setUserID(user.getId());
        String proxyID = handlelog.getProxyID();
        Proxy proxy = new Proxy();
        proxy.setId(proxyID);
        proxy.setRemark(handlelog.getRemark());
        if ("完成".equals(handlelog.getActionName())) {
            proxy.setStatus("编写报告");
        } else {
            proxy.setStatus("等待指派实验人员");
        }
        handlelogService.insert(handlelog);
        proxyService.update(proxy);
        return jsonResult;
    }

    /**
     * 报告组员添加事件跟踪记录
     *
     * @return jsonResult
     * @throws Exception
     */
    @RequestMapping(value = "insertFirstReportHandleLog", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult insertFirstReportHandleLog(HttpSession session, Handlelog handlelog, String userID) throws Exception {
        JSONResult jsonResult = new JSONResult();
        User user = (User) session.getAttribute(ManageContainer.manage_session_user_info);
        handlelog.setUserID(user.getId());
        String proxyID = handlelog.getProxyID();
        Proxy proxy = new Proxy();
        proxy.setId(proxyID);
        proxy.setRemark(handlelog.getRemark());
        if("n".equals(handlelog.getIsSecond())){
            if ("提交".equals(handlelog.getActionName())) {
                String[] userIDS = userID.split(",");
                proxy.setStatus("一审中");
                for (int i = 0; i < userIDS.length; i++) {
                    Proxyfirstcheck proxyfirstcheck = new Proxyfirstcheck();
                    proxyfirstcheck.setProxyID(proxyID);
                    proxyfirstcheck.setUserID(userIDS[i]);
                    Proxyfirstcheck rs = new Proxyfirstcheck();
                    rs.setProxyID(proxyID);
                    rs.setUserID(userIDS[i]);
                    rs = proxyfirstcheckService.selectOne(rs);
                    if (rs == null)
                        proxyfirstcheckService.insert(proxyfirstcheck);
                }
            } else {
                proxy.setStatus("实验分析");
            }
        }else{
            if ("提交".equals(handlelog.getActionName())) {
                String[] userIDS = userID.split(",");
                proxy.setStatus("二审中");
                for (int i = 0; i < userIDS.length; i++) {
                    Proxysecondcheck proxysecondcheck = new Proxysecondcheck();
                    proxysecondcheck.setProxyID(proxyID);
                    proxysecondcheck.setUserID(userIDS[i]);
                    Proxysecondcheck rs = new Proxysecondcheck();
                    rs.setProxyID(proxyID);
                    rs.setUserID(userIDS[i]);
                    rs = proxysecondcheckService.selectOne(rs);
                    if (rs == null)
                        proxysecondcheckService.insert(proxysecondcheck);
                }
            } else {
                proxy.setStatus("实验分析");
            }
        }
        handlelogService.insert(handlelog);
        proxyService.update(proxy);
        return jsonResult;
    }

    /**
     * 报告组长添加事件跟踪记录
     *
     * @return jsonResult
     * @throws Exception
     */
    @RequestMapping(value = "insertSecondReportHandleLog", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult insertSecondReportHandleLog(HttpSession session, Handlelog handlelog, String userID) throws Exception {
        JSONResult jsonResult = new JSONResult();
        User user = (User) session.getAttribute(ManageContainer.manage_session_user_info);
        handlelog.setUserID(user.getId());
        String proxyID = handlelog.getProxyID();
        Proxy proxy = new Proxy();
        proxy.setId(proxyID);
        proxy.setRemark(handlelog.getRemark());
        if("n".equals(handlelog.getIsFinish())){
            if ("提交".equals(handlelog.getActionName())) {
                String[] userIDS = userID.split(",");
                proxy.setStatus("二审中");
                for (int i = 0; i < userIDS.length; i++){
                    Proxysecondcheck proxysecondcheck = new Proxysecondcheck();
                    proxysecondcheck.setProxyID(proxyID);
                    proxysecondcheck.setUserID(userIDS[i]);
                    Proxysecondcheck rs = new Proxysecondcheck();
                    rs.setProxyID(proxyID);
                    rs.setUserID(userIDS[i]);
                    rs = proxysecondcheckService.selectOne(rs);
                    if (rs == null){
                        proxysecondcheckService.insert(proxysecondcheck);
                    }
                }
            } else {
                proxy.setStatus("编写报告");
            }
        }else{
            if ("提交".equals(handlelog.getActionName())) {
                String[] userIDS = userID.split(",");
                proxy.setStatus("发放打印");
                for (int i = 0; i < userIDS.length; i++) {
                    Proxycentralll proxycentralll = new Proxycentralll();
                    proxycentralll.setProxyID(proxyID);
                    proxycentralll.setUserID(userIDS[i]);
                    Proxycentralll rs = new Proxycentralll();
                    rs.setProxyID(proxyID);
                    rs.setUserID(userIDS[i]);
                    rs = proxycentralllService.selectOne(rs);
                    if (rs == null)
                        proxycentralllService.insert(proxycentralll);
                }
            } else {
                proxy.setStatus("编写报告");
            }
        }
        handlelogService.insert(handlelog);
        proxyService.update(proxy);
        return jsonResult;
    }

    /**
     * 报告终审人员添加事件跟踪记录
     *
     * @return jsonResult
     * @throws Exception
     */
    @RequestMapping(value = "insertFinishReportHandleLog", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult insertFinishReportHandleLog(HttpSession session, Handlelog handlelog, String userID) throws Exception {
        JSONResult jsonResult = new JSONResult();
        User user = (User) session.getAttribute(ManageContainer.manage_session_user_info);
        handlelog.setUserID(user.getId());
        String proxyID = handlelog.getProxyID();
        Proxy proxy = new Proxy();
        proxy.setId(proxyID);
        proxy.setRemark(handlelog.getRemark());
        if ("提交".equals(handlelog.getActionName())) {
            String[] userIDS = userID.split(",");
            proxy.setStatus("发放打印");
            for (int i = 0; i < userIDS.length; i++) {
                Proxycentralll proxycentralll = new Proxycentralll();
                proxycentralll.setProxyID(proxyID);
                proxycentralll.setUserID(userIDS[i]);
                Proxycentralll rs = new Proxycentralll();
                rs.setProxyID(proxyID);
                rs.setUserID(userIDS[i]);
                rs = proxycentralllService.selectOne(proxycentralll);
                if (rs == null){
                    proxycentralllService.insert(proxycentralll);
                }
            }
        } else {
            proxy.setStatus("编写报告");
        }
        handlelogService.insert(handlelog);
        proxyService.update(proxy);
        return jsonResult;
    }
}
